package cn.zmit.kuxi.holder;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.Url;
import cn.zmit.kuxi.entity.AlreadyPublicEntity;
import cn.zmit.kuxi.image.ImageDisplayer;
import cn.zmit.kuxi.interfaces.OnListItemSelectListener;

import com.xdroid.functions.holder.ViewHolderBase;

/** * @author chenxu @parameter * @return */
public class AlreadyPublicHorlder extends ViewHolderBase<AlreadyPublicEntity> {
	private TextView title, goodsName, noWin, winPhone, time;
	private ImageView goodsPic;
	private LinearLayout llRoot;
	private Context context;
	private static OnListItemSelectListener<AlreadyPublicEntity> onlistItemSelect;
	private TextView tv_luckynum;
	private TextView tv_join_time;

	@SuppressLint("InflateParams")
	@Override
	public View createView(LayoutInflater layoutInflater) {
		context = layoutInflater.getContext();
		View view = layoutInflater.inflate(R.layout.item_join_already, null);
		title = (TextView) view.findViewById(R.id.tv_title);
		goodsName = (TextView) view.findViewById(R.id.tv_goodsName);
		noWin = (TextView) view.findViewById(R.id.tv_noWin);
		winPhone = (TextView) view.findViewById(R.id.tv_winPhone);
		time = (TextView) view.findViewById(R.id.tv_time);
		goodsPic = (ImageView) view.findViewById(R.id.imag_goodsPic);
		llRoot = (LinearLayout) view.findViewById(R.id.llRoot);
		tv_join_time = (TextView) view.findViewById(R.id.tv_join_time);
		return view;
	}

	@Override
	public void showData(final int position, final AlreadyPublicEntity itemData) {
		if (itemData.getUrl() != null && itemData.getUrl().length() > 0) {
			ImageDisplayer.getInstance().dispalyImageWithRadius(
					Url.IMAGE_PICTURES + itemData.getUrl(), goodsPic, 0);
		} else {
			goodsPic.setImageResource(R.drawable.ic_launcher);
		}
		int detime = itemData.getOldtime() - itemData.getNowtime();
		if (detime > 0) {
			noWin.setVisibility(View.GONE);
			title.setText("第" + itemData.getTitle() + "期");
			goodsName.setText(itemData.getName());
			winPhone.setText("敬请期待...");
			time.setText("揭晓时间：" + "等待中...");
			llRoot.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Toast.makeText(context, "即将揭晓，敬请期待！", 0).show();
				}
			});
		} else {
			title.setText("第" + itemData.getTitle() + "期");
			goodsName.setText(itemData.getName());
			tv_join_time.setText("参与次数:" + itemData.getJointime() + "次");
			if (itemData.getUserId().equals(itemData.getWinner_id())) {
				noWin.setVisibility(View.GONE);
				int num = Integer.parseInt(itemData.getLucky_num());
				int luckNum = num;
				winPhone.setText("中奖用户:" + itemData.getWinner_phone() + "(幸运号"
						+ luckNum + ")");
			} else {
				noWin.setVisibility(View.GONE);
				int num = Integer.parseInt(itemData.getLucky_num());
				int luckNum = num;
				winPhone.setText("中奖用户:" + itemData.getWinner_phone() + "(幸运号"
						+ luckNum + ")");
			}
			llRoot.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (onlistItemSelect != null) {
						onlistItemSelect.onlistItemSelect(position, itemData);
					}
				}
			});
			time.setText("揭晓时间:" + itemData.getTime());
		}
	}

	// 设置回调
	public static void setOnListItemSelectListener(
			OnListItemSelectListener<AlreadyPublicEntity> onListItemSelectListener) {
		AlreadyPublicHorlder.onlistItemSelect = onListItemSelectListener;
	}

}
